const { mysql } = require('../qcloud')

/**
 * 响应 GET 请求（响应微信配置时的签名检查请求）
 */
async function list(ctx, next) {

  const {page=1,size=20 } = ctx.request.query

  const data = await mysql('cSessionInfo').select('*').orderBy('last_visit_time', 'desc').limit(size).offset((page - 1) * size);

  ctx.body = data.map(user => {
    user.user_info = JSON.parse(user.user_info)
    return user;
  })
}

async function findById(ctx, next) {
  const { id } = ctx.params;
  const datas = await mysql('cSessionInfo').select('*').where({ open_id: id })
  if(datas.length>0){
    const user = datas[0]
    user.user_info = JSON.parse(user.user_info)
    ctx.body = user
  }else{
    ctx.body = null
  }
}

module.exports = {
  findById,
  list,
};
